//
//  VRMainViewController.swift
//  Sina_MicoBlog
//
//  Created by Lee on 16/5/10.
//  Copyright © 2016年 Lee.C. All rights reserved.
//
//这个主要是自定义一个tabbarController出来然后让AppDelegate成为他的rootViewController,然后这个tabbarController就创建一个navigationController来加入tableViewController来做子控制器,然后,设置他的子控制器的名字,和图片.
//然后发现问题,要有个中间的按钮,所以我们需要自定义tabbar,这个属性,而这个属性是只读得,所以我们创建了一个tabbar的类,然后用KVC来将控制器的tabbar赋值给这个tabbar这个类.

    //然后我们要到这个tabbar中了


import UIKit

class VRMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
   
        
        let mainBar = VRMainTabBar()
        
        setValue(mainBar, forKey: "tabBar")
        
        let buttonB = {() -> () in
            
            print("你点击了中间按钮")
        }
        
        mainBar.addButton = buttonB
        
       settingTabbarM()
    }

    func settingTabbarM (){
    
   
        let HomeVC = VRHomeViewController()
        self.settingTabar(HomeVC, name: "首页", imageName: "tabbar_home")
        
        let MessageVC = VRMessageViewController()
        self.settingTabar(MessageVC, name: "信息", imageName: "tabbar_message_center")
        
        let DiscoveryVC = VRDiscoveryViewController()
        self.settingTabar(DiscoveryVC, name: "发现", imageName: "tabbar_discover")
        
        let ProfileVC = VRProfileViewController()
        self.settingTabar(ProfileVC, name: "我", imageName: "tabbar_profile")
    
    }

    func settingTabar ( controller: UITableViewController,name: String,imageName: String) {
        
        
        let NaviVC = UINavigationController(rootViewController: controller)
        
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        
        controller.title = name
        
        controller.tabBarItem.image  = UIImage(named: imageName)
        
        addChildViewController(NaviVC)
        
   
    }
    
}
